package com.ny.service.impl;

import com.ny.entity.User;
import com.ny.mapper.USerMapper;
import com.ny.service.EmailService;
import com.ny.service.UserService;
import com.ny.until.PasswordUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private USerMapper uSerMapper;
    @Autowired
    private EmailService emailService;
    @Override
    public List<User> findALLUser(){
        return uSerMapper.findAllUser();
    }
    @Override
    public User login(String username,String password){
        return uSerMapper.login(username,password);
    }
    @Override
    public User fingUserByUsername(String username){
        return uSerMapper.findUserByUsername(username);
    }

    @Override
    public int registerUser(User user){
        String password=user.getPassword();
        String pwd= PasswordUntil.encryptPassword(password);
        user.setPassword(pwd);
        return uSerMapper.insertUser(user.getUsername(),user.getPassword());
    }

    @Override
    public User emailExist(String email) {
        return null;
    }

    @Override
    public User findUserByEmail(String email) {
        return uSerMapper.findUserByEmail(email);
    }

    @Override
    public void sendEmailCode(String email) {
        // 检查邮箱号是否存在
        User user = uSerMapper.findUserByEmail(email);
        if (user != null){
            //生成验证码
            String code = emailService.generateCode();
            // 把验证码存储到redis数据中,进行验证和过期的
//            ValueOperations<String, String> ops = redisTemplate.opsForValue();
//            ops.set(email, code);
            emailService.sendEmail(email, code);
        }

    }

}










